home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / PowerLisp 2.01 / Supplemental Documentation / Documentation / Chapter 04. Type Specifiers < prev    next >
Text File  |  1995-03-27  |  43KB  |  979 lines

  1. Common Lisp the Language, 2nd Edition
  2. -------------------------------------------------------------------------------
  3.  
  4. 4. Type Specifiers
  5.  
  6. In Common Lisp, types are named by Lisp objects, specifically symbols and
  7. lists, called type specifiers. Symbols name predefined classes of objects,
  8. whereas lists usually indicate combinations or specializations of simpler
  9. types. Symbols or lists may also be abbreviations for types that could be
  10. specified in other ways.
  11.  
  12. -------------------------------------------------------------------------------
  13.  
  14.    *  Type Specifier Symbols
  15.    *  Type Specifier Lists
  16.    *  Predicating Type Specifiers
  17.    *  Type Specifiers That Combine
  18.    *  Type Specifiers That Specialize
  19.    *  Type Specifiers That Abbreviate
  20.    *  Defining New Type Specifiers
  21.    *  Type Conversion Function
  22.    *  Determining the Type of an Object
  23.    *  Type Upgrading
  24.  
  25. -------------------------------------------------------------------------------
  26.  
  27.  
  28. 4.1 Type Specifier Symbols
  29.  
  30. The type symbols defined by the system include those shown in table 4-1. In
  31. addition, when a structure type is defined using defstruct, the name of the
  32. structure type becomes a valid type symbol.
  33.  
  34. [change_begin]
  35. Notice of correction. In the first edition, the type specifiers signed-byte and
  36. unsigned-byte were inadvertently omitted from table 4-1.
  37. [change_end]
  38.  
  39. [change_begin]
  40. X3J13 voted in March 1989 (COMMON-TYPE)   to eliminate the type common; this
  41. fact is indicated by the brackets around the common type specifier in the
  42. table.
  43.  
  44. X3J13 voted in March 1989 (CHARACTER-PROPOSAL)   to eliminate the type
  45. string-char; this fact is indicated by the brackets around the string-char type
  46. specifier in the table.
  47.  
  48. X3J13 voted in March 1989 (CHARACTER-PROPOSAL)   to add the type
  49. extended-character and the type base-character.
  50.  
  51. X3J13 voted in March 1989 (REAL-NUMBER-TYPE)   to add the type specifier real.
  52.  
  53. X3J13 votes have also implicitly added many other type specifiers as names of
  54. classes (see chapter 28) or of conditions (see chapter 29).
  55. [change_end]
  56.  
  57.  
  58. 4.2. Type Specifier Lists
  59.  
  60. If a type specifier is a list, the car of the list is a symbol, and the rest of
  61. the list is subsidiary type information. In many cases a subsidiary item may be
  62. unspecified. The unspecified subsidiary item is indicated by writing *. For
  63. example, to completely specify a vector type, one must mention the type of the
  64. elements and the length of the vector, as for example
  65.  
  66. (vector double-float 100)
  67.  
  68. To leave the length unspecified, one would write
  69.  
  70. (vector double-float *)
  71.  
  72. To leave the element type unspecified, one would write
  73.  
  74. (vector * 100)
  75.  
  76. [change_begin]
  77. One may also leave both length and element type unspecified:
  78.  
  79. (vector * *)
  80.  
  81. [change_end]
  82.  
  83. Suppose that two type specifiers are the same except that the first has a *
  84. where the second has a more explicit specification. Then the second denotes a
  85. subtype of the type denoted by the first.
  86.  
  87. As a convenience, if a list has one or more unspecified items at the end, such
  88. items may simply be dropped rather than writing an explicit * for each one. If
  89. dropping all occurrences of * results in a singleton list, then the parentheses
  90. may be dropped as well (the list may be replaced by the symbol in its car). For
  91. example, (vector double-float *) may be abbreviated to (vector double-float),
  92. and (vector * *) may be abbreviated to (vector) and then to simply vector.
  93.  
  94.  
  95. 4.3. Predicating Type Specifiers
  96.  
  97. A type specifier list (satisfies predicate-name) denotes the set of all objects
  98. that satisfy the predicate named by predicate-name, which must be a symbol
  99. whose global function definition is a one-argument predicate. (A name is
  100. required; lambda-expressions are disallowed in order to avoid scoping
  101. problems.) For example, the type (satisfies numberp) is the same as the type
  102. number. The call (typep x '(satisfies p)) results in applying p to x and
  103. returning t if the result is true and nil if the result is false.
  104.  
  105. [old_change_begin]
  106. As an example, the type string-char could be defined as
  107.  
  108. (deftype string-char ()
  109.   '(and character (satisfies string-char-p)))
  110.  
  111. See deftype.
  112. [old_change_end]
  113.  
  114. [change_begin]
  115. X3J13 voted in March 1989 (COMMON-TYPE)   to remove the type string-char and
  116. the function string-char-p from the language.
  117. [change_end]
  118.  
  119. It is not a good idea for a predicate appearing in a satisfies type specifier
  120. to cause any side effects when invoked.
  121.  
  122.  
  123. 4.4. Type Specifiers That Combine
  124.  
  125. The following type specifier lists define a type in terms of other types or
  126. objects.
  127.  
  128. (member object1 object2 ...)
  129.      This denotes the set containing precisely those objects named. An object
  130.      is of this type if and only if it is eql to one of the specified objects.
  131.  
  132. -------------------------------------------------------------------------------
  133. Compatibility note: This is roughly equivalent to the Interlisp DECL package's
  134. memq.
  135. -------------------------------------------------------------------------------
  136.  
  137. [change_begin]
  138.  
  139. (eql object)
  140.      X3J13 voted in June 1988 (CLOS)   to add the eql type specifier. It may be
  141.      used as a parameter specializer for CLOS methods (see section 28.1.6.2 and
  142.      find-method). It denotes the set of the one object named; an object is of
  143.      this type if and only if it is eql to object. While (eql object) denotes
  144.      the same type as (member object), only (eql object) may be used as a CLOS
  145.      parameter specializer.
  146.  
  147. [change_end]
  148.  
  149. (not type)
  150.      This denotes the set of all those objects that are not of the specified
  151.      type.
  152.  
  153. (and type1 type2 ...)
  154.      This denotes the intersection of the specified types.
  155.  
  156. -------------------------------------------------------------------------------
  157. Compatibility note: This is roughly equivalent to the Interlisp DECL package's
  158. allof.
  159. -------------------------------------------------------------------------------
  160.  
  161.      When typep processes an and type specifier, it always tests each of the
  162.      component types in order from left to right and stops processing as soon
  163.      as one component of the intersection has been found to which the object in
  164.      question does not belong. In this respect an and type specifier is similar
  165.      to an executable and form. The purpose of this similarity is to allow a
  166.      satisfies type specifier to depend on filtering by previous type
  167.      specifiers. For example, suppose there were a function primep that takes
  168.      an integer and says whether it is prime. Suppose also that it is an error
  169.      to give any object other than an integer to primep. Then the type
  170.      specifier
  171.  
  172.      (and integer (satisfies primep))
  173.  
  174.      is guaranteed never to result in an error because the function primep will
  175.      not be invoked unless the object in question has already been determined
  176.      to be an integer.
  177.  
  178. (or type1 type2 ...)
  179.      This denotes the union of the specified types. For example, the type list
  180.      by definition is the same as (or null cons). Also, the value returned by
  181.      the function position is always of type (or null (integer 0 *)) (either
  182.      nil or a non-negative integer).
  183.  
  184. -------------------------------------------------------------------------------
  185. Compatibility note: This is roughly equivalent to the Interlisp DECL package's
  186. oneof.
  187. -------------------------------------------------------------------------------
  188.  
  189.      As for and, when typep processes an or type specifier, it always tests
  190.      each of the component types in order from left to right and stops
  191.      processing as soon as one component of the union has been found to which
  192.      the object in question belongs.
  193.  
  194.  
  195. 4.5. Type Specifiers That Specialize
  196.  
  197. Some type specifier lists denote specializations of data types named by
  198. symbols. These specializations may be reflected by more efficient
  199. representations in the underlying implementation. As an example, consider the
  200. type (array short-float). Implementation A may choose to provide a specialized
  201. representation for arrays of short floating-point numbers, and implementation B
  202. may choose not to.
  203.  
  204. If you should want to create an array for the express purpose of holding only
  205. short-float objects, you may optionally specify to make-array the element type
  206. short-float. This does not require make-array to create an object of type
  207. (array short-float); it merely permits it. The request is construed to mean
  208. ``Produce the most specialized array representation capable of holding
  209. short-floats that the implementation can provide.'' Implementation A will then
  210. produce a specialized array of type (array short-float), and implementation B
  211. will produce an ordinary array of type (array t).
  212.  
  213. If one were then to ask whether the array were actually of type (array
  214. short-float), implementation A would say ``yes,'' but implementation B would
  215. say ``no.'' This is a property of make-array and similar functions: what you
  216. ask for is not necessarily what you get.
  217.  
  218. [old_change_begin]
  219. Types can therefore be used for two different purposes: declaration and
  220. discrimination. Declaring to make-array that elements will always be of type
  221. short-float permits optimization. Similarly, declaring that a variable takes on
  222. values of type (array short-float) amounts to saying that the variable will
  223. take on values that might be produced by specifying element type short-float to
  224. make-array. On the other hand, if the predicate typep is used to test whether
  225. an object is of type (array short-float), only objects actually of that
  226. specialized type can satisfy the test; in implementation B no object can pass
  227. that test.
  228. [old_change_end]
  229.  
  230. [change_begin]
  231. X3J13 voted in January 1989 (ARRAY-TYPE-ELEMENT-TYPE-SEMANTICS)   to eliminate
  232. the differing treatment of types when used ``for discrimination'' rather than
  233. ``for declaration'' on the grounds that implementors have not treated the
  234. distinction consistently and (which is more important) users have found the
  235. distinction confusing.
  236.  
  237. As a consequence of this change, the behavior of typep and subtypep on array
  238. and complex type specifiers must be modified. See the descriptions of those
  239. functions. In particular, under their new behavior, implementation B would say
  240. ``yes,'' agreeing with implementation A, in the discussion above.
  241.  
  242. Note that the distinction between declaration and discrimination remains
  243. useful, if only so that we may remark that the specialized (list) form of the
  244. function type specifier may still be used only for declaration and not for
  245. discrimination.
  246.  
  247. X3J13 voted in June 1988 (FUNCTION-TYPE)   to clarify that while the
  248. specialized form of the function type specifier (a list of the symbol function
  249. possibly followed by argument and value type specifiers) may be used only for
  250. declaration, the symbol form (simply the name function) may be used for
  251. discrimination.
  252. [change_end]
  253.  
  254. The valid list-format names for data types are as follows:
  255.  
  256. (array element-type dimensions)
  257.      This denotes the set of specialized arrays whose elements are all members
  258.      of the type element-type and whose dimensions match dimensions. For
  259.      declaration purposes, this type encompasses those arrays that can result
  260.      by specifying element-type as the element type to the function make-array;
  261.      this may be different from what the type means for discrimination
  262.      purposes. element-type must be a valid type specifier or unspecified.
  263.      dimensions may be a non-negative integer, which is the number of
  264.      dimensions, or it may be a list of non-negative integers representing the
  265.      length of each dimension (any dimension may be unspecified instead), or it
  266.      may be unspecified. For example:
  267.  
  268.      (array integer 3)           ;Three-dimensional arrays of integers
  269.      (array integer (* * *))     ;Three-dimensional arrays of integers
  270.      (array * (4 5 6))           ;4-by-5-by-6 arrays
  271.      (array character (3 *))     ;Two-dimensional arrays of characters
  272.                                  ; that have exactly three rows
  273.      (array short-float ())      ;Zero-rank arrays of short-format
  274.                                  ; floating-point numbers
  275.  
  276.      Note that (array t) is a proper subset of (array *). The reason is that
  277.      (array t) is the set of arrays that can hold any Common Lisp object (the
  278.      elements are of type t, which includes all objects). On the other hand,
  279.      (array *) is the set of all arrays whatsoever, including, for example,
  280.      arrays that can hold only characters. Now (array character) is not a
  281.      subset of (array t); the two sets are in fact disjoint because (array
  282.      character) is not the set of all arrays that can hold characters but
  283.      rather the set of arrays that are specialized to hold precisely characters
  284.      and no other objects. To test whether an array foo can hold a character,
  285.      one should not use
  286.  
  287.      (typep foo '(array character))
  288.  
  289.      but rather
  290.  
  291.      (subtypep 'character (array-element-type foo))
  292.  
  293.      See array-element-type.
  294.  
  295.      [change_begin]
  296.      X3J13 voted in January 1989 (ARRAY-TYPE-ELEMENT-TYPE-SEMANTICS)   to
  297.      change typep and subtypep so that the specialized array type specifier
  298.      means the same thing for discrimination as for declaration: it encompasses
  299.      those arrays that can result by specifying element-type as the element
  300.      type to the function make-array. Under this interpretation (array
  301.      character) might be the same type as (array t) (although it also might not
  302.      be the same). See upgraded-array-element-type. However,
  303.  
  304.      (typep foo '(array character))
  305.  
  306.      is still not a legitimate test of whether the array foo can hold a
  307.      character; one must still say
  308.  
  309.      (subtypep 'character (array-element-type foo))
  310.  
  311.      to determine that question.
  312.  
  313.      X3J13 also voted in January 1989 (DECLARE-ARRAY-TYPE-ELEMENT-REFERENCES)
  314.      to specify that within the lexical scope of an array type declaration, it
  315.      is an error for an array element, when referenced, not to be of the exact
  316.      declared element type. A compiler may, for example, treat every reference
  317.      to an element of a declared array as if the reference were surrounded by a
  318.      the form mentioning the declared array element type (not the upgraded
  319.      array element type). Thus
  320.  
  321.      (defun snarf-hex-digits (the-array)
  322.        (declare (type (array (unsigned-byte 4) 1) the-array))
  323.        (do ((j (- (length array) 1) (- j 1))
  324.             (val 0 (logior (ash val 4)
  325.                            (aref the-array j))))
  326.            ((< j 0) val)))
  327.  
  328.      may be treated as
  329.  
  330.      (defun snarf-hex-digits (the-array)
  331.        (declare (type (array (unsigned-byte 4) 1) the-array))
  332.        (do ((j (- (length array) 1) (- j 1))
  333.             (val 0 (logior (ash val 4)
  334.                            (the (unsigned-byte 4)
  335.                                 (aref the-array j)))))
  336.            ((< j 0) val)))
  337.  
  338.      The declaration amounts to a promise by the user that the aref will never
  339.      produce a value outside the interval 0 to 15, even if in that particular
  340.      implementation the array element type (unsigned-byte 4) is upgraded to,
  341.      say, (unsigned-byte 8). If such upgrading does occur, then values outside
  342.      that range may in fact be stored in the-array, as long as the code in
  343.      snarf-hex-digits never sees them.
  344.  
  345.      As a general rule, a compiler would be justified in transforming
  346.  
  347.      (aref (the (array elt-type ...) a) ...)
  348.  
  349.      into
  350.  
  351.      (the elt-type (aref (the (array elt-type ...) a) ...)
  352.  
  353.      It may also make inferences involving more complex functions, such as
  354.      position or find. For example, find applied to an array always returns
  355.      either nil or an object whose type is the element type of the array.
  356.  
  357. [change_end]
  358.  
  359. (simple-array element-type dimensions)
  360.      This is equivalent to (array element-type dimensions) except that it
  361.      additionally specifies that objects of the type are simple arrays (see
  362.      section 2.5).
  363.  
  364. (vector element-type size)
  365.      This denotes the set of specialized one-dimensional arrays whose elements
  366.      are all of type element-type and whose lengths match size. This is
  367.      entirely equivalent to (array element-type (size)). For example:
  368.  
  369.      (vector double-float)     ;Vectors of double-format
  370.                                ; floating-point numbers
  371.      (vector * 5)              ;Vectors of length 5
  372.      (vector t 5)              ;General vectors of length 5
  373.      (vector (mod 32) *)       ;Vectors of integers between 0 and 31
  374.  
  375. [old_change_begin]
  376.  
  377.      The specialized types (vector string-char) and (vector bit) are so useful
  378.      that they have the special names string and bit-vector. Every
  379.      implementation of Common Lisp must provide distinct representations for
  380.      these as distinct specialized data types.
  381.  
  382. [old_change_end]
  383.  
  384. [change_begin]
  385.  
  386.      X3J13 voted in March 1989 (CHARACTER-PROPOSAL)   to eliminate the type
  387.      string-char and to redefine the type string to be the union of one or more
  388.      specialized vector types, the types of whose elements are subtypes of the
  389.      type character.
  390.  
  391. [change_end]
  392.  
  393. (simple-vector size)
  394.      This is the same as (vector t size) except that it additionally specifies
  395.      that its elements are simple general vectors.
  396.  
  397. (complex type)
  398.      Every element of this type is a complex number whose real part and
  399.      imaginary part are each of type type. For declaration purposes, this type
  400.      encompasses those complex numbers that can result by giving numbers of the
  401.      specified type to the function complex; this may be different from what
  402.      the type means for discrimination purposes. As an example, Gaussian
  403.      integers might be described as (complex integer), even in implementations
  404.      where giving two integers to the function complex results in an object of
  405.      type (complex rational).
  406.  
  407. [change_begin]
  408.  
  409.      X3J13 voted in January 1989 (ARRAY-TYPE-ELEMENT-TYPE-SEMANTICS)   to
  410.      change typep and subtypep so that the specialized complex type specifier
  411.      means the same thing for discrimination purposes as for declaration
  412.      purposes. See upgraded-complex-part-type.
  413.  
  414. [change_end]
  415.  
  416. (function (arg1-type arg2-type ...) value-type)
  417.      This type may be used only for declaration and not for discrimination;
  418.      typep will signal an error if it encounters a specifier of this form.
  419.      Every element of this type is a function that accepts arguments at least
  420.      of the types specified by the argj-type forms and returns a value that is
  421.      a member of the types specified by the value-type form. The &optional,
  422.      &rest, and &key markers may appear in the list of argument types. The
  423.      value-type may be a values type specifier in order to indicate the types
  424.      of multiple values.
  425.  
  426. [change_begin]
  427.  
  428.      X3J13 voted in January 1989 (FUNCTION-TYPE-REST-LIST-ELEMENT)   to specify
  429.      that the arg-type that follows a &rest marker indicates the type of each
  430.      actual argument that would be gathered into the list for a &rest
  431.      parameter, and not the type of the &rest parameter itself (which is always
  432.      list). Thus one might declare the function gcd to be of type (function
  433.      (&rest integer) integer), or the function aref to be of type (function
  434.      (array &rest fixnum) t).
  435.  
  436.      X3J13 voted in March 1988 (FUNCTION-TYPE-KEY-NAME)   to specify that, in a
  437.      function type specifier, an argument type specifier following &key must be
  438.      a list of two items, a keyword and a type specifier. The keyword must be a
  439.      valid keyword-name symbol that may be supplied in the actual arguments of
  440.      a call to the function, and the type specifier indicates the permitted
  441.      type of the corresponding argument value. (The keyword-name symbol is
  442.      typically a keyword, but another X3J13 vote
  443.      (KEYWORD-ARGUMENT-NAME-PACKAGE)   allows it to be any symbol.)
  444.      Furthermore, if &allow-other-keys is not present, the set of keyword-names
  445.      mentioned in the function type specifier may be assumed to be exhaustive;
  446.      for example, a compiler would be justified in issuing a warning for a
  447.      function call using a keyword argument name not mentioned in the type
  448.      declaration for the function being called. If &allow-other-keys is present
  449.      in the function type specifier, other keyword arguments may be supplied
  450.      when calling a function of the indicated type, and if supplied such
  451.      arguments may possibly be used.
  452.  
  453. [change_end]
  454.  
  455. [old_change_begin]
  456.  
  457.      As an example, the function cons is of type (function (t t) cons), because
  458.      it can accept any two arguments and always returns a cons. The function
  459.      cons is also of type (function (float string) list), because it can
  460.      certainly accept a floating-point number and a string (among other
  461.      things), and its result is always of type list (in fact a cons is never
  462.      null, but that does not matter for this type declaration). The function
  463.      truncate is of type (function (number number) (values number number)), as
  464.      well as of type (function (integer (mod 8)) integer).
  465.  
  466. [old_change_end]
  467.  
  468. [change_begin]
  469.  
  470.      X3J13 voted in January 1989 (FUNCTION-TYPE-ARGUMENT-TYPE-SEMANTICS)   to
  471.      alter the meaning of the function type specifier when used in type and
  472.      ftype declarations. While the preceding formulation may be theoretically
  473.      elegant, they have found that it is not useful to compiler implementors
  474.      and that it is not the interpretation that users expect. X3J13 prescribed
  475.      instead the following interpretation of declarations.
  476.  
  477.      A declaration specifier of the form
  478.  
  479.      (ftype (function (arg1-type arg2-type ... argn-type) value-type) fname)
  480.  
  481.      implies that any function call of the form
  482.  
  483.      (fname arg1 arg2 ...)
  484.  
  485.      within the scope of the declaration can be treated as if it were rewritten
  486.      to use the-forms in the following manner:
  487.  
  488.      (the value-type
  489.           (fname (the arg1-type arg1)
  490.                  (the arg2-type arg2)
  491.                  ...
  492.                  (the argn-type argn)))
  493.  
  494.      That is, it is an error for any of the actual arguments not to be of its
  495.      specified type arg-type or for the result not to be of the specified type
  496.      value-type. (In particular, if any argument is not of its specified type,
  497.      then the result is not guaranteed to be of the specified type-if indeed a
  498.      result is returned at all.)
  499.  
  500.      Similarly, a declaration specifier of the form
  501.  
  502.      (type (function (arg1-type arg2-type ... argn-type) value-type) var)
  503.  
  504.      is interpreted to mean that any reference to the variable var will find
  505.      that its value is a function, and that it is an error to call this
  506.      function with any actual argument not of its specified type arg-type.
  507.      Also, it is an error for the result not to be of the specified type
  508.      value-type. For example, a function call of the form
  509.  
  510.      (funcall var arg1 arg2 ...)
  511.  
  512.      could be rewritten to use the-forms as well. If any argument is not of its
  513.      specified type, then the result is not guaranteed to be of the specified
  514.      type-if indeed a result is returned at all.
  515.  
  516.      Thus, a type or ftype declaration specifier describes type requirements
  517.      imposed on calls to a function as opposed to requirements imposed on the
  518.      definition of the function. This is analogous to the treatment of type
  519.      declarations of variables as imposing type requirements on references to
  520.      variables, rather than on the contents of variables. See the vote of X3J13
  521.      on type declaration specifiers in general, discussed in section 9.2.
  522.  
  523.      In the same manner as for variable type declarations in general, if two or
  524.      more of these declarations apply to the same function call (which can
  525.      occur if declaration scopes are suitably nested), then they all apply; in
  526.      effect, the types for each argument or result are intersected. For
  527.      example, the code fragment
  528.  
  529.      (locally (declare (ftype (function (biped) digit)
  530.                               butcher-fudge))
  531.        (locally (declare (ftype (function (featherless) opposable)
  532.                                 butcher-fudge))
  533.          (butcher-fudge sam)))
  534.  
  535.      may be regarded as equivalent to
  536.  
  537.      (the opposable
  538.           (the digit (butcher-fudge (the featherless
  539.                                          (the biped sam)))))
  540.  
  541.      or to
  542.  
  543.      (the (and opposable digit)
  544.           (butcher-fudge (the (and featherless biped) sam)))
  545.  
  546.      That is, sam had better be both featherless and a biped, and the result of
  547.      butcher-fudge had better be both opposable and a digit; otherwise the code
  548.      is in error. Therefore a compiler may generate code that relies on these
  549.      type assumptions, for example.
  550.  
  551. [change_end]
  552.  
  553. (values value1-type value2-type ...)
  554.      This type specifier is extremely restricted: it may be used only as the
  555.      value-type in a function type specifier or in a the special form. It is
  556.      used to specify individual types when multiple values are involved. The
  557.      &optional, &rest, and &key markers may appear in the value-type list; they
  558.      thereby indicate the parameter list of a function that, when given to
  559.      multiple-value-call along with the values, would be suitable for receiving
  560.      those values.
  561.  
  562.  
  563. 4.6. Type Specifiers That Abbreviate
  564.  
  565. The following type specifiers are, for the most part, abbreviations for other
  566. type specifiers that would be far too verbose to write out explicitly (using,
  567. for example, member).
  568.  
  569. (integer low high)
  570.      Denotes the integers between low and high. The limits low and high must
  571.      each be an integer, a list of an integer, or unspecified. An integer is an
  572.      inclusive limit, a list of an integer is an exclusive limit, and * means
  573.      that a limit does not exist and so effectively denotes minus or plus
  574.      infinity, respectively. The type fixnum is simply a name for (integer
  575.      smallest largest) for implementation-dependent values of smallest and
  576.      largest (see most-negative-fixnum and most-positive-fixnum). The type
  577.      (integer 0 1) is so useful that it has the special name bit.
  578.  
  579. (mod n)
  580.      Denotes the set of non-negative integers less than n. This is equivalent
  581.      to (integer 0 n-1) or to (integer 0 (n)).
  582.  
  583. (signed-byte s)
  584.      Denotes the set of integers that can be represented in two's-complement
  585.      form in a byte of s bits. This is equivalent to (integer      ). Simply
  586.      signed-byte or (signed-byte *) is the same as integer.
  587.  
  588. (unsigned-byte s)
  589.      Denotes the set of non-negative integers that can be represented in a byte
  590.      of s bits. This is equivalent to (mod   ), that is, (integer 0   ). Simply
  591.      unsigned-byte or (unsigned-byte *) is the same as (integer 0 *), the set
  592.      of non-negative integers.
  593.  
  594. (rational low high)
  595.      Denotes the rationals between low and high. The limits low and high must
  596.      each be a rational, a list of a rational, or unspecified. A rational is an
  597.      inclusive limit, a list of a rational is an exclusive limit, and * means
  598.      that a limit does not exist and so effectively denotes minus or plus
  599.      infinity, respectively.
  600.  
  601. (float low high)
  602.      Denotes the set of floating-point numbers between low and high. The limits
  603.      low and high must each be a floating-point number, a list of a
  604.      floating-point number, or unspecified; a floating-point number is an
  605.      inclusive limit, a list of a floating-point number is an exclusive limit,
  606.      and * means that a limit does not exist and so effectively denotes minus
  607.      or plus infinity, respectively.
  608.  
  609.      In a similar manner, one may use:
  610.  
  611.      (short-float low high)
  612.      (single-float low high)
  613.      (double-float low high)
  614.      (long-float low high)
  615.  
  616.      In this case, if a limit is a floating-point number (or a list of one), it
  617.      must be one of the appropriate format.
  618.  
  619. [change_begin]
  620. X3J13 voted in March 1989 (REAL-NUMBER-TYPE)   to add a list form of the real
  621. type specifier to denote an interval of real numbers.
  622.  
  623. (real low high)
  624.      Denotes the real numbers between low and high. The limits low and high
  625.      must each be a real, a list of a real, or unspecified. A real is an
  626.      inclusive limit, a list of a real is an exclusive limit, and * means that
  627.      a limit does not exist and so effectively denotes minus or plus infinity,
  628.      respectively.
  629.  
  630. [change_end]
  631.  
  632. [old_change_begin]
  633.  
  634. (string size)
  635.      Means the same as (array string-char (size)): the set of strings of the
  636.      indicated size.
  637.  
  638. (simple-string size)
  639.      Means the same as (simple-array string-char (size)): the set of simple
  640.      strings of the indicated size.
  641.  
  642. [old_change_end]
  643.  
  644. [change_begin]
  645. X3J13 voted in March 1989 (CHARACTER-PROPOSAL)   to eliminate the type
  646. string-char and to redefine the type string to be the union of one or more
  647. specialized vector types, the types of whose elements are subtypes of the type
  648. character. Similarly, the type simple-string is redefined to be the union of
  649. one or more specialized simple vector types, the types of whose elements are
  650. subtypes of the type character.
  651.  
  652. (base-string size)
  653.      Means the same as (vector base-character size): the set of base strings of
  654.      the indicated size.
  655.  
  656. (simple-base-string size)
  657.      Means the same as (simple-array base-character (size)): the set of simple
  658.      base strings of the indicated size.
  659.  
  660. [change_end]
  661.  
  662. (bit-vector size)
  663.      Means the same as (array bit (size)): the set of bit-vectors of the
  664.      indicated size.
  665.  
  666. (simple-bit-vector size)
  667.      This means the same as (simple-array bit (size)): the set of bit-vectors
  668.      of the indicated size.
  669.  
  670.  
  671. 4.7. Defining New Type Specifiers
  672.  
  673. New type specifiers can come into existence in two ways. First, defining a new
  674. structure type with defstruct automatically causes the name of the structure to
  675. be a new type specifier symbol. Second, the deftype special form can be used to
  676. define new type-specifier abbreviations.
  677.  
  678. [Macro]
  679. deftype name lambda-list [[{declaration}* | doc-string]] {form}*
  680.  
  681. This is very similar to a defmacro form: name is the symbol that identifies the
  682. type specifier being defined, lambda-list is a lambda-list (and may contain
  683. &optional and &rest markers), and the forms constitute the body of the expander
  684. function. If we view a type specifier list as a list containing the type
  685. specifier name and some argument forms, the argument forms (unevaluated) are
  686. bound to the corresponding parameters in lambda-list. Then the body forms are
  687. evaluated as an implicit progn, and the value of the last form is interpreted
  688. as a new type specifier for which the original specifier was an abbreviation.
  689. The name is returned as the value of the deftype form.
  690.  
  691. deftype differs from defmacro in that if no initform is specified for an
  692. &optional parameter, the default value is *, not nil.
  693.  
  694. If the optional documentation string doc-string is present, then it is attached
  695. to the name as a documentation string of type type; see documentation.
  696.  
  697. Here are some examples of the use of deftype:
  698.  
  699. (deftype mod (n) `(integer 0 (,n)))
  700. (deftype list () '(or null cons))
  701.  
  702. (deftype square-matrix (&optional type size)
  703.   "SQUARE-MATRIX includes all square two-dimensional arrays."
  704.   `(array ,type (,size ,size)))
  705.  
  706. (square-matrix short-float 7)  means  (array short-float (7 7))
  707.  
  708. (square-matrix bit)  means  (array bit (* *))
  709.  
  710. If the type name defined by deftype is used simply as a type specifier symbol,
  711. it is interpreted as a type specifier list with no argument forms. Thus, in the
  712. example above, square-matrix would mean (array * (* *)), the set of
  713. two-dimensional arrays. This would unfortunately fail to convey the constraint
  714. that the two dimensions be the same; (square-matrix bit) has the same problem.
  715. A better definition is
  716.  
  717. (defun equidimensional (a)
  718.   (or (< (array-rank a) 2)
  719.       (apply #'= (array-dimensions a))))
  720.  
  721. (deftype square-matrix (&optional type size)
  722.   `(and (array ,type (,size ,size))
  723.         (satisfies equidimensional)))
  724.  
  725. [change_begin]
  726. X3J13 voted in March 1988 (FLET-IMPLICIT-BLOCK)   to specify that the body of
  727. the expander function defined by deftype is implicitly enclosed in a block
  728. construct whose name is the same as the name of the defined type. Therefore
  729. return-from may be used to exit from the function.
  730.  
  731. X3J13 voted in March 1989 (DEFINING-MACROS-NON-TOP-LEVEL)   to clarify that,
  732. while defining forms normally appear at top level, it is meaningful to place
  733. them in non-top-level contexts; deftype must define the expander function
  734. within the enclosing lexical environment, not within the global environment.
  735. [change_end]
  736.  
  737.  
  738. 4.8. Type Conversion Function
  739.  
  740. The following function may be used to convert an object to an equivalent object
  741. of another type.
  742.  
  743. [Function]
  744. coerce object result-type
  745.  
  746. The result-type must be a type specifier; the object is converted to an
  747. ``equivalent'' object of the specified type. If the coercion cannot be
  748. performed, then an error is signaled. In particular, (coerce x 'nil) always
  749. signals an error. If object is already of the specified type, as determined by
  750. typep, then it is simply returned. It is not generally possible to convert any
  751. object to be of any type whatsoever; only certain conversions are permitted:
  752.  
  753.    *  Any sequence type may be converted to any other sequence type, provided
  754.      the new sequence can contain all actual elements of the old sequence (it
  755.      is an error if it cannot). If the result-type is specified as simply
  756.      array, for example, then (array t) is assumed. A specialized type such as
  757.      string or (vector (complex short-float)) may be specified; of course, the
  758.      result may be of either that type or some more general type, as determined
  759.      by the implementation. Elements of the new sequence will be eql to
  760.      corresponding elements of the old sequence. If the sequence is already of
  761.      the specified type, it may be returned without copying it; in this,
  762.      (coerce sequence type) differs from (concatenate type sequence), for the
  763.      latter is required to copy the argument sequence. In particular, if one
  764.      specifies sequence, then the argument may simply be returned if it already
  765.      is a sequence.
  766.  
  767.      (coerce '(a b c) 'vector) => #(a b c)
  768.  
  769. [change_begin]
  770. X3J13 voted in June 1989 (SEQUENCE-TYPE-LENGTH)   to specify that coerce should
  771. signal an error if the new sequence type specifies the number of elements and
  772. the old sequence has a different length.
  773.  
  774. X3J13 voted in March 1989 (CHARACTER-PROPOSAL)   to specify that if the
  775. result-type is string then it is understood to mean (vector character), and
  776. simple-string is understood to mean (simple-array character (*)).
  777. [change_end]
  778.  
  779. [old_change_begin]
  780.  
  781.    *  Some strings, symbols, and integers may be converted to characters. If
  782.      object is a string of length 1, then the sole element of the string is
  783.      returned. If object is a symbol whose print name is of length 1, then the
  784.      sole element of the print name is returned. If object is an integer n,
  785.      then (int-char n) is returned. See character.
  786.  
  787.      (coerce "a" 'character) => #\a
  788.  
  789. [old_change_end]
  790.  
  791. [change_begin]
  792. X3J13 voted in March 1989 (CHARACTER-PROPOSAL)   to eliminate int-char from
  793. Common Lisp. Presumably this eliminates the possibility of coercing an integer
  794. to a character, although the vote did not address this question directly.
  795. [change_end]
  796.  
  797.    *  Any non-complex number can be converted to a short-float, single-float,
  798.      double-float, or long-float. If simply float is specified, and object is
  799.      not already a float of some kind, then the object is converted to a
  800.      single-float.
  801.  
  802.      (coerce 0 'short-float) => 0.0S0
  803.      (coerce 3.5L0 'float) => 3.5L0
  804.      (coerce 7/2 'float) => 3.5
  805.  
  806.    *  Any number can be converted to a complex number. If the number is not
  807.      already complex, then a zero imaginary part is provided by coercing the
  808.      integer zero to the type of the given real part. (If the given real part
  809.      is rational, however, then the rule of canonical representation for
  810.      complex rationals will result in the immediate re-conversion of the result
  811.      from type complex back to type rational.)
  812.  
  813.      (coerce 4.5s0 'complex) => #C(4.5S0 0.0S0)
  814.      (coerce 7/2 'complex) => 7/2
  815.      (coerce #C(7/2 0) '(complex double-float))
  816.         => #C(3.5D0 0.0D0)
  817.  
  818.    *  Any object may be coerced to type t.
  819.  
  820.      (coerce x 't) == (identity x) == x
  821.  
  822. [change_begin]
  823. X3J13 voted in June 1988 (FUNCTION-TYPE)   to allow coercion of certain objects
  824. to the type function:
  825.  
  826.    *  A symbol or lambda-expression can be converted to a function. A symbol is
  827.      coerced to type function as if by applying symbol-function to the symbol;
  828.      an error is signaled if the predicate fboundp is not true of the symbol or
  829.      if the symbol names a macro or special form. A list x whose car is the
  830.      symbol lambda is coerced to a function as if by execution of (eval `#',x),
  831.      that is, of (eval (list 'function x)).
  832.  
  833. [change_end]
  834.  
  835. Coercions from floating-point numbers to rationals and from ratios to integers
  836. are purposely not provided because of rounding problems. The functions
  837. rational, rationalize, floor, ceiling, truncate, and round may be used for such
  838. purposes. Similarly, coercions from characters to integers are purposely not
  839. provided; char-code or char-int may be used explicitly to perform such
  840. conversions.
  841.  
  842.  
  843. 4.9. Determining the Type of an Object
  844.  
  845. The following function may be used to obtain a type specifier describing the
  846. type of a given object.
  847.  
  848. [Function]
  849. type-of object
  850.  
  851. [old_change_begin]
  852. (type-of object) returns an implementation-dependent result: some type of which
  853. the object is a member. Implementors are encouraged to arrange for type-of to
  854. return the most specific type that can be conveniently computed and is likely
  855. to be useful to the user. If the argument is a user-defined named structure
  856. created by defstruct, then type-of will return the type name of that structure.
  857. Because the result is implementation-dependent, it is usually better to use
  858. type-of primarily for debugging purposes; however, in a few situations portable
  859. code requires the use of type-of, such as when the result is to be given to the
  860. coerce or map function. On the other hand, often the typep function or the
  861. typecase construct is more appropriate than type-of.
  862. [old_change_end]
  863.  
  864. -------------------------------------------------------------------------------
  865. Compatibility note: In MacLisp the function type-of is called typep, and
  866. anomalously so, for it is not a predicate.
  867. -------------------------------------------------------------------------------
  868.  
  869. [change_begin]
  870. Many have observed (and rightly so) that this specification is totally wimpy
  871. and therefore nearly useless. X3J13 voted in June 1989
  872. (TYPE-OF-UNDERCONSTRAINED)   to place the following constraints on type-of:
  873.  
  874.    *  Let x be an object such that (typep x type) is true and type is one of
  875.      the following:
  876.  
  877.      array           float         package         sequence
  878.      bit-vector      function      pathname        short-float
  879.      character       hash-table    random-state    single-float
  880.      complex         integer       ratio           stream
  881.      condition       long-float    rational        string
  882.      cons            null          readtable       symbol
  883.      double-float    number        restart         vector
  884.  
  885.      Then (subtypep (type-of x) type)) must return the values t and t; that is,
  886.      type-of applied to x must return either type itself or a subtype of type
  887.      that subtypep can recognize in that implementation.
  888.  
  889.    *  For any object x, (subtypep (type-of x) (class-of x)) must produce the
  890.      values t and t.
  891.  
  892.    *  For every object x, (typep x (type-of x)) must be true. (This implies
  893.      that type-of can never return nil, for no object is of type nil.)
  894.  
  895.    *  type-of never returns t and never uses a satisfies, and, or, not, or
  896.      values type specifier in its result.
  897.  
  898.    *  For objects of CLOS metaclass structure-class or of standard-class,
  899.      type-of returns the proper name of the class returned by class-of if it
  900.      has a proper name, and otherwise returns the class itself. In particular,
  901.      for any object created by a defstruct constructor function, where the
  902.      defstruct had the name name and no :type option, type-of will return name.
  903.  
  904. As an example, (type-of "acetylcholinesterase") may return string or
  905. simple-string or (simple-string 20), but not array or simple-vector. As another
  906. example, it is permitted for (type-of 1729) to return integer or fixnum (if it
  907. is indeed a fixnum) or (signed-byte 16) or (integer 1729 1729) or (integer 1685
  908. 1750) or even (mod 1730), but not rational or number, because
  909.  
  910. (typep (+ (expt 9 3) (expt 10 3)) 'integer)
  911.  
  912. is true, integer is in the list of types mentioned above, and
  913.  
  914. (subtypep (type-of (+ (expt 1 3) (expt 12 3))) 'integer)
  915.  
  916. would be false if type-of were to return rational or number.
  917. [change_end]
  918.  
  919.  
  920. [change_begin]
  921.  
  922. 4.10. Type Upgrading
  923.  
  924. X3J13 voted in January 1989 (ARRAY-TYPE-ELEMENT-TYPE-SEMANTICS)   to add new
  925. functions by which a program can determine, in a given Common Lisp
  926. implementation, how that implementation will upgrade a type when constructing
  927. an array specialized to contain elements of that type, or a complex number
  928. specialized to contain parts of that type.
  929.  
  930. [Function]
  931. upgraded-array-element-type type
  932.  
  933. A type specifier is returned, indicating the element type of the most
  934. specialized array representation capable of holding items of the specified
  935. argument type. The result is necessarily a supertype of the given type.
  936. Furthermore, if a type A is a subtype of type B, then
  937. (upgraded-array-element-type A) is a subtype of (upgraded-array-element-type
  938. B).
  939.  
  940. The manner in which an array element type is upgraded depends only on the
  941. element type as such and not on any other property of the array such as size,
  942. rank, adjustability, presence or absence of a fill pointer, or displacement.
  943.  
  944. -------------------------------------------------------------------------------
  945. Rationale: If upgrading were allowed to depend on any of these properties, all
  946. of which can be referred to, directly or indirectly, in the language of type
  947. specifiers, then it would not be possible to displace an array in a consistent
  948. and dependable manner to another array created with the same :element-type
  949. argument but differing in one of these properties.
  950. -------------------------------------------------------------------------------
  951.  
  952. Note that upgraded-array-element-type could be defined as
  953.  
  954. (defun upgraded-array-element-type (type)
  955.   (array-element-type (make-array 0 :element-type type)))
  956.  
  957. but this definition has the disadvantage of allocating an array and then
  958. immediately discarding it. The clever implementor surely can conjure up a more
  959. practical approach.
  960.  
  961. [Function]
  962. upgraded-complex-part-type type
  963.  
  964. A type specifier is returned, indicating the element type of the most
  965. specialized complex number representation capable of having parts of the
  966. specified argument type. The result is necessarily a supertype of the given
  967. type. Furthermore, if a type A is a subtype of type B, then
  968. (upgraded-complex-part-type A) is a subtype of (upgraded-complex-part-type B).
  969. [change_end]
  970.  
  971.  
  972.  
  973.  
  974.  
  975.  
  976.  
  977.  
  978.  
  979.